class_name ShopCard
extends Panel

signal on_item_purchased(item: ItemBase)

@export var shop_item: ItemBase: set = _set_shop_item

@onready var item_icon: TextureRect = %ItemIcon
@onready var item_name: Label = %ItemName
@onready var item_type: Label = %ItemType
@onready var item_description: RichTextLabel = %ItemDescription
@onready var coins_label: Label = %CoinsLabel

func _set_shop_item(value: ItemBase) -> void:
	shop_item = value
	item_icon.texture = shop_item.item_icon
	item_name.text = shop_item.item_name
	item_description.text = shop_item.get_description()
	item_type.text = ItemBase.ItemType.keys()[shop_item.item_type]
	coins_label.text = str(shop_item.item_cost)
	
	add_theme_stylebox_override("panel", Global.get_tier_style(shop_item.item_tier))


func _on_buy_button_pressed() -> void:
	SoundManager.player_sound(SoundManager.Sound.UI)
	if Global.equipped_weapons.size() >= Global.MAX_EQUIP_COUNT and shop_item.item_type == ItemBase.ItemType.WEAPON:
		return
	if Global.coins > shop_item.item_cost:
		Global.coins -= shop_item.item_cost
		on_item_purchased.emit(shop_item)
		queue_free()
